package com.xingyun.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.xingyun.app.data.entity.TodayArticleResponse;
import com.xingyun.mvp.contract.MainPagerContract;
import com.xingyun.mvp.ui.home.adapter.MainPagerAdapter;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class MainPagerPresenter extends BasePresenter<MainPagerContract.Model, MainPagerContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private MainPagerAdapter mAdapter;

    private boolean isFirst = true;

    private List<TodayArticleResponse.DataBean> mArticles;

    String date = getCurrentDate();

    @Inject
    public MainPagerPresenter(MainPagerContract.Model model, MainPagerContract.View rootView
            , RxErrorHandler handler, Application application, MainPagerAdapter adapter
            , ImageLoader imageLoader, AppManager appManager,List<TodayArticleResponse.DataBean> articles) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        this.mAdapter = adapter;
        this.mArticles = articles;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void getArticle(final boolean pullToRefresh) {
        if (pullToRefresh)
            date = getCurrentDate();//下拉刷新默认只请求第一页

        boolean isEvictCache = pullToRefresh;//是否驱逐缓存,为ture即不使用缓存,每次下拉刷新即需要最新数据,则不使用缓存

        if (pullToRefresh && isFirst) {//默认在第一次下拉刷新时使用缓存
            isFirst = false;
            isEvictCache = false;
        }

        mModel.getArticle(date)
                .retryWhen(new RetryWithDelay(3, 2))
                .doOnSubscribe(disposable -> {
                    if (pullToRefresh)
                        mRootView.showLoading();//显示下拉刷新的进度条
                    else
                        mRootView.startLoadMore();//显示上拉加载更多的进度条
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (pullToRefresh)
                        mRootView.hideLoading();//隐藏下拉刷新的进度条
                    else
                        mRootView.endLoadMore();//隐藏上拉加载更多的进度条
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<TodayArticleResponse>(mErrorHandler) {
                    @Override
                    public void onNext(TodayArticleResponse todayArticleResponse) {
                        TodayArticleResponse.DataBean dataBean = todayArticleResponse.getData();
                        date = dataBean.getDate().getPrev();
                        if (pullToRefresh) mArticles.clear();//如果是下拉刷新则清空列表
                        mArticles.add(dataBean);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    private String getCurrentDate(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
         return sdf.format(new java.util.Date());
    }
}
